package com.sairobo.live.controller;

import com.sairobo.live.api.UserService;
import com.google.common.util.concurrent.RateLimiter;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Function:user控制器
 * @author crossoverJie
 * Date: 2017/6/7 下午11:55
 * @since JDK 1.8
 */
@RestController
@RequestMapping(value = "/hehe")
@Api(value = "userApi+", description = "用户API+", tags = {"用户服务"})
public class UserController {
    private final static Logger logger = LoggerFactory.getLogger(UserController.class);

//    @Autowired
//    UserServiceClient userServiceClient;

    @Autowired
    UserService userService;


    @ApiOperation("获取用户")
    @RequestMapping(value = "/getUser", method = RequestMethod.POST)
    @HystrixCommand(fallbackMethod = "hiError")//优先级别低于@FeignClient中的fallback,如果userService没有配置fallback的话这个熔断才会生效
    public String getuser(){
        RateLimiter limiter = RateLimiter.create(2.0) ;
        userService.getUserByFeignBatch();
        double acquire = limiter.acquire();
        logger.info("获取令牌成功!,消耗=" + acquire);
        return "呵呵，调用成功@";
    }


    @RequestMapping(value ="/hi", method = {RequestMethod.GET})
    public String hi(){
        return userService.getUserByHystrix();
    }

    public String hiError() {
        return "hi,sorry,error!";
    }
}
